package com.dubbo.lian.cms.modules.app.service.impl;
 import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dubbo.lian.common.util.BaseR;
import com.dubbo.lian.cms.modules.app.UserPicture;
import com.dubbo.lian.cms.modules.app.mapper.UserPictureMapper;
import com.dubbo.lian.cms.modules.app.service.IUserPictureService;
import com.dubbo.lian.common.util.ImageUtil;
import com.dubbo.lian.common.util.JsonUtil;
import com.dubbo.lian.common.util.ReturnTemplateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
 import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 /**
 * <p>
 * 用户相册。  服务实现类
 * </p>
 *
 * @author lian
 * @since 2016-12-26
 */
@Service
public class UserPictureServiceImpl extends ServiceImpl<UserPictureMapper, UserPicture> implements IUserPictureService {
     private Logger logger = LoggerFactory.getLogger(UserPictureServiceImpl.class);
     @Override
    public BaseR insertUserPictuer(String userID, byte[] fileByte){
        String userid = userID;
         if (userid == null||"".equals(userid)){
            return ReturnTemplateUtil.errReturn("空的userid,坑爹",logger);
        }
         Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String TimeStr = sdf.format(date);
         String path="E:/AppResource/UserPicture/"+userid+"/";
        String fileName=TimeStr;
        try {
            long start = System.currentTimeMillis();
            ImageUtil.saveUploadImageAndThumbnail(fileByte,path,fileName);
            long end = System.currentTimeMillis();
            System.out.println(end-start);
            String fullPicture = userid+"/"+fileName+".jpg";
            String thumbnail = userid+"/"+fileName+"_t.jpg";
            UserPicture userPicture = new UserPicture();
            userPicture.setUserid(userid);
            userPicture.setThumbnail(thumbnail);
            userPicture.setFullPicture(fullPicture);
            userPicture.setIsDel(0);
            userPicture.insert();
        } catch (IOException e) {
            return ReturnTemplateUtil.errReturn(e.getMessage(),logger);
        }
        return ReturnTemplateUtil.returnOfNull();
     }
     @Override
    public BaseR selectUserPicturePage(JSONObject obj){
        String userid = obj.getString("userid");
        int pagNum = obj.getIntValue("pagNum");
        int pageSize = obj.getIntValue("pageSize");
        if(pagNum<=0){
            pagNum=1;
            pageSize=20;
        }
         EntityWrapper ew = new EntityWrapper();
        ew.where("userid = {0}",userid);
        ew.and("is_del = {0}",0);
        Page<UserPicture> page= new Page<>(pagNum, pageSize);
        List<UserPicture> records = super.selectPage(page,ew).getRecords();
         records.forEach(e->{
            e.setFullPicture("http://120.76.47.124:10000/UserPicture/"+e.getFullPicture());
            e.setThumbnail("http://120.76.47.124:10000/UserPicture/"+e.getThumbnail());
        });
        JSONArray pictureJsonArray = JsonUtil.jsonTimeFormat(records,"yyyy-MM-dd HH:mm:ss");
        Map pictureList = new HashMap();
        pictureList.put("pictureList",pictureJsonArray);
        return ReturnTemplateUtil.returnOfMap(pictureList);
    }
     @Override
    public BaseR deleteUserPicture(UserPicture obj) {
        try {
            UserPicture userPicture = new UserPicture();
            userPicture.setIsDel(1);
            userPicture.setId(obj.getId());
            userPicture.updateById();
        } catch (Exception e) {
            return  ReturnTemplateUtil.errReturn(e.getMessage(),logger);
        }
        return ReturnTemplateUtil.returnOfNull();
    }
}
